<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="11" style="border-right:1px solid #dfe4ed">
        <el-form :model="queryParams1" ref="queryForm1" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="学生姓名" prop="name">
            <el-input
              v-model="queryParams1.studentName"
              placeholder="请输入学生姓名"
              clearable
              @keyup.enter.native="handleQuery1"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery1">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery1">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table height="800" v-loading="loading1" :data="leftStudentList" @selection-change="handleSelectionChange1">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="学生姓名" align="center" prop="name" />
          <el-table-column label="性别" align="center" prop="gender" />
          <el-table-column label="生日" align="center" prop="birthday" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否就学" align="center" prop="effective">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.effective" :value="scope.row.effective"/>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          :total="total1"
          :page.sync="queryParams1.pageNum"
          :limit.sync="queryParams1.pageSize"
          @pagination="getList1"
        />
      </el-col>
      <el-col :span="2">
        <div class="transfer">
          <div class="transfer-button">
            <el-button type="success" icon="el-icon-arrow-right" @click="associate">添加学生</el-button>
          </div>
          <div class="transfer-button">
            <el-button type="danger" icon="el-icon-arrow-left" @click="unAssociate">移除学生</el-button>
          </div>
        </div>
      </el-col>
      <el-col :span="11" style="border-left:1px solid #dfe4ed">
        <el-form :model="queryParams2" ref="queryForm2" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="学生姓名" prop="name">
            <el-input
              v-model="queryParams2.studentName"
              placeholder="请输入学生姓名"
              clearable
              @keyup.enter.native="handleQuery2"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery2">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery2">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table height="800" v-loading="loading2" :data="rightStudentList" @selection-change="handleSelectionChange2">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="学生姓名" align="center" prop="name" />
          <el-table-column label="性别" align="center" prop="gender" />
          <el-table-column label="生日" align="center" prop="birthday" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否就学" align="center" prop="effective">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.effective" :value="scope.row.effective"/>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          :total="total2"
          :page.sync="queryParams2.pageNum"
          :limit.sync="queryParams2.pageSize"
          @pagination="getList2"
        />
      </el-col>
    </el-row>

  </div>
</template>

<script>
import {unAssociatedStudentList,associatedStudentList,associate,unAssociate} from "@/api/ysg/course"

export default {
  name: "Student",
  dicts: ['effective'],
  data() {
    return {
      // 遮罩层
      loading1: true,
      loading2: true,
      // 选中数组
      ids1: [],
      ids2: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total1: 0,
      total2: 0,
      // 学生管理表格数据
      leftStudentList: [],
      rightStudentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
      },
      // 表单参数
      form: {},
      courseId: null,
    }
  },
  created() {
    this.courseId = this.$route.params && this.$route.params.courseId
    if(this.courseId) {
      this.getList1()
      this.getList2()
    }else {
      this.$modal.notifyError("参数有误");
    }

  },
  methods: {
    /** 查询学生管理列表 */
    getList1() {
      this.loading1 = true
      this.queryParams1.courseId = this.courseId
      unAssociatedStudentList(this.queryParams1).then(response => {
        this.leftStudentList = response.rows
        this.total1 = response.total
        this.loading1 = false
      })
    },
    getList2() {
      this.loading2 = true
      this.queryParams2.courseId = this.courseId
      associatedStudentList(this.queryParams2).then(response => {
        this.rightStudentList = response.rows
        this.total2 = response.total
        this.loading2 = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        gender: null,
        birthday: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        effective: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery1() {
      this.queryParams1.pageNum = 1
      this.getList1()
    },
    handleQuery2() {
      this.queryParams2.pageNum = 1
      this.getList2()
    },
    /** 重置按钮操作 */
    resetQuery1() {
      this.queryParams1.studentName = null
      this.handleQuery1()
    },
    /** 重置按钮操作 */
    resetQuery2() {
      this.queryParams2.studentName = null
      this.handleQuery2()
    },
    // 多选框选中数据
    handleSelectionChange1(selection) {
      this.ids1 = selection.map(item => item.id)
    },
    // 多选框选中数据
    handleSelectionChange2(selection) {
      this.ids2 = selection.map(item => item.id)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加学生管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改学生管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除学生管理编号为"' + ids + '"的数据项？').then(function() {
        return delStudent(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ysg/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    },
    associate() {
      if(this.ids1.length < 1) {
        this.$modal.msgWarning("请在左侧表格中勾选需要添加的学生")
        return
      }
      const param = {}
      param.courseId = this.courseId
      param.studentIds = this.ids1
      associate(param).then(res => {
        this.$modal.msgSuccess("添加成功")
        this.ids1 = []
        this.getList1()
        this.getList2()
      })
    },
    unAssociate() {
      if(this.ids2.length < 1) {
        this.$modal.msgWarning("请在右侧表格中勾选需要移除的学生")
        return
      }
      const param = {}
      param.courseId = this.courseId
      param.studentIds = this.ids2
      unAssociate(param).then(res => {
        this.$modal.msgSuccess("移除成功")
        this.ids2 = []
        this.getList1()
        this.getList2()
      })
    }
  }
}
</script>
<style scoped>
.transfer-button {
  width: 100%;
  text-align: center;
  margin-top: 20px;
}
</style>
